local japi = require 'jass.japi'
local jass = require 'jass.common'

local model = {}
setmetatable(model,model)

--- 存放所有model
model.allmodels = {}

local mt = extends(class.ui_base){}
model.__index = mt 

--- 控件类型
mt.type = 'model'

--- 魔兽中的类型
mt._type = 'SPRITE'

--- 识别名
mt._name = '模型'

--- 模型路径
mt.model = [[UI\Glues\MainMenu\WarCraftIIILogo\WarCraftIIILogo.mdl]]

--- fdf
mt.fdf = "model"

--- 模型缩放
mt.size = 1.0

--- 模型颜色
mt.color = 0xffffffff

--- 模型动画速度
mt.speed = 1.0

--- 模型偏移坐标X (如果模型显示在左下角则可以设置 offset_x = model.parent.w/2)
mt.offset_x = 0

--- 模型偏移坐标Y (如果模型显示在左下角则可以设置 offset_y = -1 * model.parent.h/2)
mt.offset_y = 0

--- 动画ID 
mt.animation = 1

--- 动画进度
mt.progress = 0

--- 模型贴图替换表 {[id] = "image_path"}
mt.textures = nil 

--- 模型缩放表 {x,y,z,}
--mt.scales = nil
mt.scales = {1,1,1}

--- 模型旋转表 {x,y,z,}
--mt.rotates = nil
mt.rotates = {0,0,0} 

--- 是否循环播放
mt.is_loop = true

--- 屏蔽事件
mt.ignore_flag = true 


--- 创建控件
function mt.new(parent, model,x,y, w, h)
    return class.model:builder
    {
        parent = parent,
        model = model,
        x = x,
        y = y,
        w = w,
        h = h,
    }
end 

--- 转换
function mt.convert(handle, parent, is_default)
    local control = model.allmodels[handle]
    if control then 
        return control
    end 
    control = class.ui_base.convert(handle, 'model', parent, is_default)
    model.allmodels[handle] = control
    return control
end
--- 初始化
function mt:init()
    if self.handle then return end 
    class.ui_base.init(self)
    local handle = japi.CreateFrameByTagName( self._type, self._name, self.parent.handle, self.fdf, self._index )
    if not handle or handle == 0 then
        class.ui_base.remove(self)
        log.error('创建model失败')
        return
    end
    self.handle = handle 
    model.allmodels[handle] = self
    class.ui_base.allframes[handle] = self
    self:reset()
    if self.show_flag then
        self.show_flag = false
        self:show()
    else 
        self.show_flag = true
        self:hide()
    end
    return self
end 

-- FramePoints_TopLeft=0,framepoints,0,左上
-- FramePoints_Top =0,framepoints,1,顶部
-- FramePoints_TopRight=0,framepoints,2,右上
-- FramePoints_Left=0,framepoints,3,左侧
-- FramePoints_Center=0,framepoints,4,中心
-- FramePoints_Right=0,framepoints,5,右侧
-- FramePoints_BottomLeft=0,framepoints,6,左下
-- FramePoints_Bottom=0,framepoints,7,底部
-- FramePoints_BottomRight=0,framepoints,8,右下

--- 重新设置
function mt:reset()
    self:set_model(self.model)
    self:replace_textures(self.textures)
    self:set_size(self.size)
    self:set_color(self.color)
    self:set_speed(self.speed)
    self:set_offset(self.offset_x,self.offset_y)
    self:set_scales(self.scales)
    self:set_rotates(self.rotates)
    self:set_progress(self.progress)
    self:set_alpha(self.alpha)
    self:set_width(self.w)
    self:set_height(self.h)
    self:set_position(self.x,self.y)
    self:set_ignore_trackevents(self.ignore_flag)
    self:play_animation(self.animation,self.is_loop)
end

--- 删除控件
function mt:remove()
    if self.removed then return end
    if self:is_simple() then return end
    model.allmodels[self.handle] = nil
    class.ui_base.remove(self)
end

--- 设置模型路径
--- @param model string 模型路径
function mt:set_model(model)
    if self.removed then return end 
    self.model = model 
    japi.FrameSetModel(self.handle,self.model,0,0)
end

--- 设置替换贴图
--- @param textures table 表
function mt:replace_textures(textures)
    if self.removed then return end 
    if not textures then return end 
    for id,texture in pairs(textures) do 
        if type(id) == 'number' and type(texture) == 'string' then 
            japi.FrameSetModelTexture(self.handle,texture,id)
        end
    end
end

--- 设置模型缩放倍率
--- @param size number 缩放
function mt:set_size(size)
    if self.removed then return end 
    japi.FrameSetModelSize(self.handle,size or 1)
end

--- 获取模型缩放倍率
--- @return number size 缩放
function mt:get_size()
    if self.removed then return self.size end 
    return japi.FrameGetModelSize(self.handle)
end 

--- 设置模型颜色
--- @param color string 颜色
function mt:set_color(color)
    if self.removed then return end 
    self.color = color 
    japi.FrameSetModelColor(self.handle,color or self.color)
end

--- 获取模型颜色
--- @return string hex
function mt:get_color()
    return self.color
end 

--- 设置动画播放倍率
--- @param speed number 播放速度
function mt:set_speed(speed)
    self.speed = speed 
    if self.removed then return end 
    japi.FrameSetModelSpeed(self.handle,self.speed)
end

--- 获取动画播放倍率
--- @return number
function mt:get_speed()
    if self.removed then return self.speed end 
    return japi.FrameGetModelSpeed(self.handle)
end

--- 设置模型与控件的偏移坐标
function mt:set_offset(offset_x,offset_y)
    self.offset_x = offset_x 
    self.offset_y = offset_y 
    if self.removed then return end 
    japi.FrameSetModelXY(self.handle,self.offset_x / 1920 * 0.8 ,(-self.offset_y / 1080) * 0.6)
end

--- 获取偏移坐标
function mt:get_offset()
    if self.removed then return self.offset_x,self.offset_y end 
    local x = japi.FrameGetModelX(self.handle) / 0.8 * 1920
    local y = japi.FrameGetModelY(self.handle) / 0.6 * 1080 * -1
    return x,y 
end

--- 按xyz轴设置模型缩放
function mt:set_scales(scales)
    if not scales then return end 
    local x,y,z = table.unpack(scales)
    japi.FrameSetModelScale(self.handle,x or 1,y or 1,z or 1)
end

--- 设置模型旋转x轴
function mt:set_rotate_x(value)
    if self.removed then return end 
    japi.FrameSetModelRotateX(self.handle,value or 0)
end

--- 设置模型旋转y轴
function mt:set_rotate_y(value)
    if self.removed then return end 
    japi.FrameSetModelRotateY(self.handle,value or 0)
end

--- 设置模型旋转z轴
function mt:set_rotate_z(value)
    if self.removed then return end 
    japi.FrameSetModelRotateZ(self.handle,value or 0)
end

--- 按xyz轴设置模型旋转
function mt:set_rotates(rotates)
    if not rotates then return end 
    local x,y,z = table.unpack(rotates)
    self:set_rotate_x(x)
    self:set_rotate_y(y)
    self:set_rotate_z(z)
end 

--- 设置动画进度
function mt:set_progress(progress)
    if self.removed then return end 
    progress = progress or 0.0
    progress = math.min(0.99, progress)
    progress = math.max(0.00, progress)
    japi.FrameSetAnimateOffset(self.handle, progress)
end

--- 播放动画
function mt:play_animation(animation,is_loop)
    if self.removed then return end 
    japi.FrameSetAnimate(self.handle,animation,is_loop == true)
end

--同单位一样的 按照索引播放指定动画  
function mt:set_animation_by_index(index)
    if self.removed then return end 
    japi.FrameSetAnimationByIndex(self.handle,index or 0)
end

class.model = mt


return mt